/***************************************************************************
 * Copyright (C) 2013 Dj_Skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 *
 * MiiButtons.cpp
 *
 * for SaveGame Manager GX
 ***************************************************************************/
#include "MiiButtons.hpp"
#include "NandMiiTask.hpp"
#include "../ManageTools.h"
#include "../../Controls/Application.h"
#include "../../Menu/Explorer.h"

enum
{
	EXTRACT_MII = 1,
	DELETE_NAND_MII,
	INSTALL_MII_TO_ENAND
};

MiiButtons::MiiButtons()
	: CommonButtons()
{
	SetENandValues();
	
	actionButtons = new ManageButtons(0,
									  0,
									  tr( "Extract" ),
									  tr( "Delete" ),
									  (!Settings.SneekRunning && Settings.EmuBrowsers && eNandCount) ? tr( "Install" ) : NULL);
	actionButtons->ButtonClicked.connect(this, &MiiButtons::OnActionClick);
	
	emuButtons = new ManageButtons(tr( "Install to:" ),
								   0,
								   emu1 ? tr( "Emu1" ) : NULL,
								   emu2 ? tr( "Emu2" ) : NULL,
								   emu3 ? tr( "Emu3" ) : NULL,
								   tr( "Cancel" ));
	emuButtons->ButtonClicked.connect(this, &MiiButtons::OnEmuClick);
	
	pathButtons = new ManageButtons(tr( "Extract to:" ), 0, tr( "Default" ), tr( "Browsed" ), tr( "Custom" ), tr( "Cancel" ));
	pathButtons->ButtonClicked.connect(this, &MiiButtons::OnPathClick);
	
	Append(actionButtons);
}

MiiButtons::~MiiButtons()
{
	RemoveAll();

	delete emuButtons;
	delete pathButtons;
}

void MiiButtons::OnActionClick(ManageButtons *buttons UNUSED, int choice)
{
	action = choice;
	BlockButtons(true);
	HideButtons(actionButtons);
	
	if(action == EXTRACT_MII)
	{
		ShowButtons(pathButtons);
	}
	else if(action == DELETE_NAND_MII)
	{
		confirmButtons->SetTitle(tr( "Delete from Wii" ));
		Settings.TmpPath = "";
		
		Confirm();
	}
	else if(action == INSTALL_MII_TO_ENAND)
	{
		if(eNandCount > 1)
		{
			ShowButtons(emuButtons);
		}
		else
		{
			confirmButtons->SetTitle(tr( "Install to:" ));
			
			if(emu1)
				Settings.TmpPath = Settings.EmuNandPath_1;
			else if(emu2)
				Settings.TmpPath = Settings.EmuNandPath_2;
			else if(emu3)
				Settings.TmpPath = Settings.EmuNandPath_3;
			
			Confirm();
		}
	}
}

void MiiButtons::OnEmuClick(ManageButtons *buttons UNUSED, int choice)
{
	HideButtons(emuButtons);
	
	if(choice < 4)
	{
		confirmButtons->SetTitle(tr( "Install to:" ));
		Settings.TmpPath = ManageTools::GetENandPath(choice);
		
		Confirm();
	}
	else
	{
		Cancel();
	}
}

void MiiButtons::OnPathClick(ManageButtons *buttons UNUSED, int choice)
{
	HideButtons(pathButtons);
	
	confirmButtons->SetTitle(tr( "Extract to:" ));
	
	if(choice == 1) //! Default
	{
		Settings.TmpPath = Settings.SaveMiisPath;
		
		Confirm();
	}
	else if(choice == 2) //! Browsed
	{
		Settings.TmpPath = Settings.LastUsedPath;
		
		Confirm();
	}
	else if(choice == 3) //! Custom
	{
		PathBrowser *pathBrowser = new PathBrowser(Settings.SaveMiisPath);
		pathBrowser->ButtonClick.connect(this, &MiiButtons::OnPathBrowserButtonClick);
	}
	else if(choice == 4) //! Cancel
	{
		Cancel();
	}
}

void MiiButtons::OnPathBrowserButtonClick(PathBrowser *menu, u8 filter UNUSED, bool doneClicked, const std::string &Path)
{
	menu->SetEffect(EFFECT_FADE, -20);
	while(menu->IsAnimated())
		Application::Instance()->updateEvents();
	menu->Close();
	
	if(doneClicked)
	{
		Settings.TmpPath = Path;
		Confirm();
	}
	else
	{
		Cancel();
	}
}

void MiiButtons::OnConfirmClick(ManageButtons *buttons UNUSED, int choice)
{
	HideButtons(confirmButtons);
	
	if(choice == 1)
	{
		NandMiiTask * task = NULL;
		
		if(action == DELETE_NAND_MII)
			task = new NandMiiTask(NandMiiTask::DELETE_FROM_NAND);
		else if(action == EXTRACT_MII)
			task = new NandMiiTask(NandMiiTask::EXTRACT_TO_DEV, Settings.TmpPath);
		else if(action == INSTALL_MII_TO_ENAND)
			task = new NandMiiTask(NandMiiTask::INSTALL_TO_ENAND, Settings.TmpPath);
		
		task->End.connect(this, &MiiButtons::OnTaskEnd);
	}
	else //! Cancel
	{
		Cancel();
	}
}

void MiiButtons::OnTaskEnd(GuiElement * task, int result)
{
	delete task;
	
	if(result > 0)
	{
		if(action == EXTRACT_MII) succesButtons->SetTitle(tr( "Succesfully extracted" ));
		else if (action == INSTALL_MII_TO_ENAND) succesButtons->SetTitle(tr( "Succesfully installed" ));
		else if (action == DELETE_NAND_MII)
		{
			deleted = true;
			succesButtons->SetTitle(tr( "Succesfully deleted" ));
		}
		
		ShowButtons(succesButtons);
	}
	else if(result == 0)
	{
		if(action == EXTRACT_MII) cancelButtons->SetTitle(tr( "Extract cancelled" ));
		else if (action == DELETE_NAND_MII) cancelButtons->SetTitle(tr( "Delete cancelled" ));
		else if (action == INSTALL_MII_TO_ENAND) cancelButtons->SetTitle(tr( "Install cancelled" ));
		
		ShowButtons(cancelButtons);
	}
	else if(result < 0)
	{
		if (action == DELETE_NAND_MII) errorButtons->SetMessage(tr( "Unable to delete datas" ));
		else errorButtons->SetMessage(tr( "Unable to copy datas" ));
		
		ShowButtons(errorButtons);
	}
}
